<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品详情</title>
    <!-- jquery -->
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <!-- bootstrap -->
    <link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.min.css"/>
    <script type="text/javascript" src="/bootstrap/js/bootstrap.min.js">
    </script>
    <!-- layer -->
    <script type="text/javascript" src="/layer/layer.js"></script>
    <!-- common.js -->
    <script type="text/javascript" src="/js/common.js"></script>

    <style>
        * {
            margin: 0;
            padding: 0;
            font-family: "Open Sans", sans-serif;
            text-transform: uppercase;
            letter-spacing: 3px;
            font-size: 11px;
        }

        body {
            background: #cbc0d3;
        }

        .main-header {
            width: 100%;
            height: 100px;
            background: #eac7cc;
            display: block;
        }

        .navbar {
            display: inline-block;
            float: right;
            margin-right: 50px;
            margin-top: 30px;
        }

        .logo {
            display: inline-block;
            margin-top: 30px;
            margin-left: 30px;
            text-decoration: none;
        }

        .logo-lg {
            font-size: 20px;
            font-weight: lighter;
            color: #232324;
        }

        .logo-lg > b {
            font-size: 20px;
            font-weight: lighter;
            color: #232324;
        }

        .container {
            background: #FFFFFF;
            margin-right: auto;
            margin-left: auto;
            width: 900px;
        }

        .captcha {
            display: none;
        }

        .captchaImg {
            display: none;
            width: 130px;
            height: 32px;
        }
    </style>
</head>
<body>


<!--抽取头部公共页面-->
<header id="site-header" class="main-header">
    <!-- Logo -->
    <a class="logo" onclick="toList()">
        <span class="logo-lg"><b>商品抢购</b>系统</span>
    </a>

    <nav class="navbar navbar-static-top">
        <!-- Sidebar toggle button-->
        <a href="#" class="sidebar-toggle" data-toggle="push-menu" role="button">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </a>

        <div class="navbar-custom-menu">
            <ul class="nav navbar-nav">

                <li class="dropdown user user-menu">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                        <img class="user-image" alt="User Image">
                        <span class="hidden-xs"></span>
                    </a>
                    <ul class="dropdown-menu">
                        <!-- User image -->
                        <li class="user-header">
                            <img class="img-circle" alt="User Image">
                            <p>
                                Team B06 - Web Developer
                                <small>2021.4.20</small>
                            </p>
                        </li>
                        <!-- Menu Body -->
                        <li class="user-body">
                        </li>
                        <li class="user-footer">
                            <div class="pull-middle">
                                <a onclick="toOut()" class="btn btn-lg btn-default btn-block">退出系统</a>
                            </div>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
    </nav>
</header>

<div class="panel panel-default">
    <div class="panel-heading">秒杀商品详情</div>
    <div class="panel-body">
<!--    <span th:if = "${user eq null}"> 您还没有登录，请登陆后再操作<br/></span>-->
        <span id="userTip"> 您还没有登录，请登陆后再操作<br/></span>
<!--        <span>没有收货地址的提示</span>-->
    </div>
    <div class="container">

        <table class="table" id="good">
            <tr>
                <td>商品名称</td>
<!--            <td colspan="3" th:text="${goods.goodsName}"></td>-->
                <td colspan="3" id="goodsName"></td>
            </tr>
            <tr>
                <td>商品图片</td>
<!--            <td colspan="3"><img th:src="@{${goods.goodsImg}}" width="200" height="200"/></td>-->
                <td colspan="3"><img id="goodsImg" width="200" height="200"/></td>
            </tr>
            <tr>
                <td>秒杀开始时间</td>
                <!--格式化时间在 common.js 中 format函数 -->
<!--            <td th:text="${#dates.format(goods.startDate,'yyyy-MM-dd HH:mm:ss')}"></td>-->
                <td id="startDate"></td>
<!--            <td id="secondKillTip">
                    <input type="hidden" id="remainSeconds" th:value="${remainSeconds}"/>
                    <span th:if="${secKillStatus eq 0}">秒杀倒计时:<span id="countDown" th:text="${remainSeconds}"></span>秒</span>
                    <span th:if="${secKillStatus eq 1}">秒杀进行中</span>
                    <span th:if="${secKillStatus eq 2}">秒杀已结束</span>
                </td>-->
                <td>
                    <input type="hidden" id="remainSeconds"/>
                    <span id="secondKillTip"></span>
                </td>
            </tr>
            <tr>
                <td>秒杀价</td>
<!--            <td colspan="3" th:text="${goods.secKillPrice}"></td>-->
                <td colspan="3" id="secKillPrice"></td>
            </tr>
            <tr>
                <td>商品原价</td>
<!--            <td colspan="3" th:text="${goods.goodsPrice}"></td>-->
                <td colspan="3" id="goodsPrice"></td>
            </tr>
            <tr>
                <td>库存数量</td>
<!--            <td colspan="3" th:text="${goods.stockCount}"></td>-->
                <td colspan="3" id="stockCount"></td>
            </tr>
            <tr>
                <td>
<!--                    <form method="get" action="/secondKill/doSecondKill">
                        <input type="hidden" name="goodsId" th:value="${goods.id}"/>
                        <input type="hidden" name="goodsId" id="goodsId"/>
                        <button class="btn btn-default btn-sm" type="submit" id="buyButton">立即秒杀</button>
                    </form>-->
                    <div>
                        <div class="form-inline">
                            <img id="captchaImg" width="120" height="30" onclick="refreshCaptcha()" class="captchaImg" style="display: none"/>
                        </div>
                    </div>
                </td>
                <td>
                    <div>
                        <input id="captcha" class="form-control" style="display: none">
                    </div>
                </td>
                <td>
                    <button class="btn btn-primary btn-block" type="button" id="buyButton" onclick="getSecKillPath()">立即秒杀
                        <input type="hidden" name="goodsId" id="goodsId"/>
                    </button>
                </td>
            </tr>
        </table>
    </div>
</div>
</body>
<script>
    $(function () {
        //秒杀倒计时
        //countDown();
        getDetails();
    });

    function getSecKillPath() {
        var goodsId = $("#goodsId").val();
        var captcha = $("#captcha").val();
        g_showLoading();

        $.ajax({
            url: "/secondKill/path",
            type: "GET",
            data: {
                goodsId: goodsId,
                captcha: captcha,
            },
            async: false,
            success: function (data) {
                if (data.code == 200) {
                    var path = data.object;
                    console.log('path:',path);
                    doSecKill(path);
                } else {
                    layer.msg("未知错误"+data.message);
                }
            },
            error: function () {
                layer.msg("获取秒杀地址请求错误");
            }
        })

    }

    function doSecKill(path) {
        $.ajax({
            url: '/secondKill/' + path + '/doSecondKill',
            type: 'POST',
            data: {
                goodsId: $("#goodsId").val()
            },
            success: function (data) {
                if (data.code == 200) {
                    getResult($("#goodsId").val());
                } else {
                    layer.msg(data.message);
                }
            },
            error: function () {
                layer.msg("客户端请求错误！", {time: 2000});
            }
        });
    }

    function getResult(goodsId) {
        g_showLoading();

        $.ajax({
            url: "/secondKill/getResult",
            type: "GET",
            data: {
                goodsId: goodsId,
            },
            success: function (data) {
                if (data.code == 200) {
                    var result = data.object;

                    console.log('result:',result);

                    if (result < 0) {
                        layer.msg("秒杀失败");
                    } else if (result == 0) {
                        setTimeout(function () {
                            getResult(goodsId);
                        }, 50);
                    } else {
                        layer.confirm("恭喜您，抢购成功！ 是否查看订单？", {btn: ["确定", "取消"]},
                            function () {
                                window.location.href = "/orderDetail.htm?orderId=" + result;
                            },
                            function () {
                                layer.close();
                            })
                    }
                }
            },
            error: function () {
                layer.msg("客户端请求错误")
            }
        })

    }
    //获取参数
    function getDetails() {
        var goodId = g_getQueryString("goodId");

        $.ajax({
            url: '/goods/detail/' + goodId,
            type: 'GET',
            success: function (data) {
                if (data.code == 200) {
                    //传输参数为GoodsDetailVo
                    render(data.object);
                } else {
                    layer.msg("返回商品详情请求出错 ");
                }
            },
            error: function () {
                layer.msg("商品详情请求出错");
            }
        });
    }
    //渲染页面
    function render(detail) {
        var user = detail.user;
        var goods = detail.goodsVo;
        var remainSeconds = detail.remainSeconds;
        if (user) {
            $("#userTip").hide();
        }
        $("#goodsName").text(goods.goodsName);
        $("#goodsImg").attr("src", goods.goodsImg);
        $("#startDate").text(new Date(goods.startDate).format("yyyy-MM-dd HH:mm:ss"));
        $("#remainSeconds").val(remainSeconds);
        $("#goodsId").val(goods.id);
        $("#goodsPrice").text(goods.goodsPrice);
        $("#secKillPrice").text(goods.secKillPrice);
        $("#stockCount").text(goods.stockCount);
        countDown();
    }
    //倒计时
    function countDown() {
        var remainSeconds = $("#remainSeconds").val();
        var timeout;
        //秒杀还没开始，倒计时
        if (remainSeconds > 0) {
            $("#buyButton").attr("disabled", true);
            $("#secondKillTip").html("秒杀倒计时：" + remainSeconds + "秒");
            timeout = setTimeout(function () {
                $("#remainSeconds").val(remainSeconds - 1);
                countDown();
            }, 1000);
        } else if (remainSeconds == 0) {
            $("#buyButton").attr("disabled", false);
            if (timeout) {
                clearTimeout(timeout);
            }
            $("#secondKillTip").html("秒杀进行中");
            $("#captchaImg").attr("src", "/secondKill/captcha?goodsId=" + $("#goodsId").val() + "&time" + new Date());
            $("#captchaImg").show();
            $("#captcha").show();
        } else {
            //秒杀已经结束
            $("#buyButton").attr("disabled", true);
            $("#secondKillTip").html("秒杀已经结束");
            $("#captchaImg").hide();
            $("#captcha").hide();
        }
    }

    function refreshCaptcha() {
        $("#captchaImg").attr("src", "/secondKill/captcha?goodsId=" + $("#goodsId").val() + "&time" + new Date());
    }
</script>
</html>